package com.app.service.utils;

import com.app.model.main.Article;
import com.app.model.main.MessageRecord;
import com.app.model.main.UserInfo;
import com.app.model.solr.SolrFlag;
import com.app.model.util.ArticleFlag;
import com.app.repository.interfaces.ArticleMapper;
import com.app.repository.interfaces.MeetRecordMapper;
import com.app.service.*;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Lichaojie on 2017/5/7. It's a beautiful day.
 */
@Service
public class SearchUtil {

    @Autowired
    private RecommendUtil recommendUtil;
    @Autowired
    private LoginRecordService loginRecordService;
    @Autowired
    private MessageRecordService messageRecordService;
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private ApprovalService approvalService;
    @Autowired
    private SolrService solrService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private MeetRecordService meetRecordService;

    /**
     * 获取某用户的活跃度分数
     * @param userId
     * @return
     */
    public double getActivity(long userId){

        //最近7天内登录的天数
        double score1;
        long num1 = loginRecordService.getLoginTimesByDay(7,userId);
        score1 = num1 / 7.0 * 10.0 ;
        if(score1 - 10.0 > 0){
            score1 = 10.0;
        }

        //最近3天内的私信用户数
        double score2;
        long num2 = messageRecordService.getMessageTimesByDay(3,userId);
        score2 = num2 - 10.0 > 0 ? 10.0 : num2;

        //7天内评论他人帖子数
        double score3;
        long num3 = commentsService.getCommentsTimesByDay(7,userId);
        score3 = num3 - 20.0 > 0 ? 20.0 : num3 / 20.0 * 10.0 ;

        //7天内点赞他人帖子或评论数
        double score4;
        long num4 = approvalService.getApprovalTimesByDay(7, userId);
        score4 = num4 - 20.0 > 0 ? 5.0 : num4 / 20.0 * 5.0;

        //同意对方线下见面数
        double score5;
        long num5 = meetRecordService.getMeetTimesByDay(7, userId);
        score5 = num5 - 3.0 > 0 ? 15.0 : num5 * 5.0;

        return score1 + score2 + score3 + score4 + score5;
    }

    /**
     * 获取某用户的匹配度分数
     * （由于 搜索部分匹配度的总分值 = 推荐部分匹配度的总分值 * 2.0，且两者计算匹配度的算法相同）
     * @param queryString
     * @param id 待匹配用户
     * @return
     */
    public double getSuitability(String queryString,long id){
        String[] strings = queryString.split(" ");
        int length = strings[0].length() + strings.length - 1;
        double totalScore = 5.0 * length;

        //匹配用户标签的得分
        double score1;
        List searchUserResult = solrService.queryWithUserId(queryString, SolrFlag.USER, 0, 10, id);
        if(searchUserResult.size() == 0){
            score1 = 0.0;
        }else {
            SolrDocument solrDocument = (SolrDocument)searchUserResult.get(0);
            double scoreOfUser = ((Float)solrDocument.getFieldValue("score")).doubleValue();
            score1 = scoreOfUser / totalScore * 50.0;

            if(score1 - 50.0 > 0){
                score1 = 50.0;
            }
        }

        //匹配用户相关分享贴的得分
        double score2 = 0;
        List<Article> articleList = articleMapper.getArticleList(id, ArticleFlag.SHARE);
        if(articleList.size() == 0){
            score2 = 0;
        }else {
            for (Article article : articleList){
                long articleId = article.getId();
                List searchArticleResult = solrService.queryWithTypeAndUserId(queryString, SolrFlag.ARTICLE, 0, 10, (byte) 1, articleId);
                if(searchArticleResult.size() == 0){
                    score2 += 0.0;
                }else {
                    SolrDocument solrDocument = (SolrDocument)searchArticleResult.get(0);
                    double scoreOfArticle = ((Float)solrDocument.getFieldValue("score")).doubleValue();
                    score2 += (scoreOfArticle / totalScore * 15);
                    if(score2 - 150.0 > 0){
                        score2 = 150.0;
                        break;
                    }
                }
            }
        }

        return score1 + score2;
    }

    /**
     * 获取某用户的权威性分数
     * (由于 搜索部分权威度的总分值 = 推荐部分权威度的总分值 * 2.0，且两者计算权威度的算法相同）
     * @param userId
     * @return
     */
    public double getAuthoritative(long userId){
        return recommendUtil.getAuthoritative(userId) * 2.0;
    }
}
